package com.afterRain.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;

import com.jfinal.kit.PathKit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.FormBody.Builder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
* @author 作者 E-mail:
* @version 创建时间：2017年5月10日 下午5:12:15
* 类说明
*/
public class OkHttpUtil {

	/**
	 * post(带参)
	 * @param url
	 * @param name
	 * @param value
	 */
	public static Response postParameter(String url , String[] name , String[] value){
		//创建okhttp对象
		OkHttpClient okHttpClient = new OkHttpClient();
		//代理设置
		//okHttpClient.newBuilder().proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(":", 9090))).build();
		Builder builder = new FormBody.Builder();
		//循环把name和value放进去,类似与表达的键值对
		if(name != null && value != null && name.length == value.length && name.length>0)
			for (int i = 0; i < value.length; i++) {
				if(name[i] != null)
					builder.add(name[i], value[i]);
			}
		//
		RequestBody body = builder.build();
		//创建这个url并带上参数
		Request request = new Request.Builder().url(url).post(body).build();
		//连接这个url
		Call call = okHttpClient.newCall(request);
		try {
			//获取返回对象
			Response response = call.execute();
			//打印获取到的值
			//System.out.println(response.body().string());
			//System.out.println(response.code());
			return response;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * okhttp GET请求方式(带参)
	 * @param url
	 * @param name
	 * @param value
	 * @return
	 */
	public static Response getParameter(String url , String[] name , String[] value){
		OkHttpClient client = new OkHttpClient();
		okhttp3.Request.Builder builder = new Request.Builder();
		if(name != null && value != null && name.length == value.length && name.length>0)
			for (int i = 0; i < value.length; i++) {
				if(name[i] != null)
					builder.header(name[i], value[i]);
			}
		Request request = builder.build();
		Call call = client.newCall(request);
		try {
			return call.execute();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	} 
	
	/**
	 * 请求url(无任何参数)
	 * @param url
	 * @return
	 */
	public static Response requestUrl(String url){
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder().url(url).build();
		Call call = client.newCall(request);
		try {
			return call.execute();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 异步的okHTTP
	 * @param url
	 */
	public static void asynchronizationRequestUrl(String url){
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder().url(url).build();
		Call call = client.newCall(request);
		call.enqueue(new Callback() {
			public void onResponse(Call call, Response response) throws IOException {
				System.out.println(Thread.currentThread().getId());
			}
			
			public void onFailure(Call call, IOException e) {
				e.printStackTrace();
			}
		});
	}

	/**
	 * 异步下载文件
	 * @param url
	 * @param name
	 */
	public static void asynchronizationDownload(String url,final String name){
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder().url(url).build();
		Call call = client.newCall(request);
		call.enqueue(new Callback() {
			public void onResponse(Call call, Response response) throws IOException {
				String src =PathKit.getRootClassPath();
				byte[] bs = response.body().bytes();
				File file = new File(src+"/"+name);
				FileOutputStream fileOutputStream= null;
				try {
					fileOutputStream = new FileOutputStream(file);
					fileOutputStream.write(bs);
					fileOutputStream.flush();
				} catch (Exception e) {
					e.printStackTrace();
				}finally{
					if(fileOutputStream != null)
						fileOutputStream.close();
				}
				
			}
			
			public void onFailure(Call call, IOException e) {
				e.printStackTrace();
			}
		});
	}
	
	/**
	 * 同步下载文件
	 * @param url
	 * @param name
	 * @throws IOException
	 */
	public static void Download(String url,final String name) throws IOException{
		Response response = requestUrl(url);
		String src =PathKit.getRootClassPath();
		byte[] bs = response.body().bytes();
		File file = new File(src+"/"+name);
		FileOutputStream fileOutputStream= null;
		try {
			fileOutputStream = new FileOutputStream(file);
			fileOutputStream.write(bs);
			fileOutputStream.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(fileOutputStream != null)
				fileOutputStream.close();
		}
	}
}
